package com.jams.goodox.activity.serve;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.ServeOrderDetailBean;
import com.jams.goodox.utils.MoneyUtils;
import com.jams.goodox.utils.OrderUtils;
import com.jams.goodox.utils.permission.PhoneUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.widget.LRTextView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 用于场地预定or停车申请订单详情基础公共类
 * Created by guozhen.hou on 2017/7/11.
 */

public abstract class ServeOrderBaseDetailActivity extends BaseActivity {

    @Bind(R.id.frame_order_status)
    FrameLayout frameOrderStatus;
    @Bind(R.id.order_state)
    TextView orderState;
    @Bind(R.id.tv_remain_time)
    TextView tvRemainTime;
    @Bind(R.id.tv_order_no)
    TextView tvOrderNo;
    @Bind(R.id.order_submit_time)
    TextView orderSubmitTime;
    @Bind(R.id.frame_address)
    FrameLayout frameAddress;
    @Bind(R.id.lr_total_money)
    LRTextView lrTotalMoney;
    @Bind(R.id.lr_rel_money)
    LRTextView lrRelMoney;
    @Bind(R.id.lr_pay_type)
    LRTextView lrPayType;
    @Bind(R.id.lr_red_packet)
    LRTextView lrRedPacket;
    @Bind(R.id.lr_invoice_type)
    LRTextView lrInvoiceType;
    @Bind(R.id.lr_invoice_name)
    LRTextView lrInvoiceName;

    @Bind(R.id.frame_serve_detail)
    protected FrameLayout frameServeDetail;//用于处理具体的ui

    private String servicePhone;//客服电话
    private  PhoneUtils phoneUtils;

    @Override
    protected int getLayoutId() {
        return R.layout.serve_order_base_detail;
    }

    @Override
    protected void init() {
        getOrderDetail();
        phoneUtils=new PhoneUtils(this);
        StyleUtil.titleBackListenerWithRightTextListener(this, "订单详情", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doActivityBack();
            }
        },"拨打客服", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callServicePhone();
            }
        });
    }

    /**
     * 拨打客服电话
     */
    protected  void callServicePhone(){
        if (Utils.isFastDoubleClick()|| TextUtils.isEmpty(servicePhone))
            return;
        phoneUtils.makeCall("客服", servicePhone, false);
    }

    /**
     * 请求订单详情数据
     */
    protected abstract void getOrderDetail();
    /**
     * 点击返回相应事件
     */
    protected abstract void doActivityBack();

    /**
     * 此处设置界面数据：由于实体对象由界面定制，可以直接设置元素
     * @param serveOrderDetailBean 获取数据后转换的实体
     */
    protected void setUiData(ServeOrderDetailBean serveOrderDetailBean){
        if(serveOrderDetailBean==null)return;
        servicePhone=serveOrderDetailBean.getServicePhone();
        setInfomationData(serveOrderDetailBean);
        setAddressView(serveOrderDetailBean);
        setDetailData(serveOrderDetailBean);
        setOrderMoneyData(serveOrderDetailBean);
        setOrderInvoiceData(serveOrderDetailBean);
        setOrderStatusData(serveOrderDetailBean);
    }

    @Override
    public void onBackPressed() {
        doActivityBack();
    }

    /**
     * 顶部订单状态，编号，时间，剩余时间ui
     * @param serveOrderDetailBean 订单页面实体
     */
    private void setInfomationData(ServeOrderDetailBean serveOrderDetailBean){
        orderState.setText(serveOrderDetailBean.getOrderStatusStr());//订单状态
        tvRemainTime.setText(serveOrderDetailBean.getEmptyPayTime());//剩余付款时间
        tvOrderNo.setText(serveOrderDetailBean.getOrderNumber());//订单编号
        orderSubmitTime.setText(serveOrderDetailBean.getSubmitOrderTime());//下单时间
    }

    /**
     * 此处设置订单地址信息
     * 区分个人地址or企业地址
     */
    private void setAddressView(ServeOrderDetailBean serveOrderDetailBean){
        frameAddress.removeAllViews();
        if(serveOrderDetailBean.isPersonOrder()){
            View personView=getLayoutInflater().inflate(R.layout.serve_order_detail_address_person,null);
            frameAddress.addView(personView);
            ServeOrderDetailBean.PersonAddressBean personAddressBean=serveOrderDetailBean.getPersonAddressBean();
            TextView tx_consignee_name= ButterKnife.findById(personView,R.id.tx_consignee_name);
            tx_consignee_name.setText(String.format("收货人：%s",personAddressBean.getConsigneePeople()));
            TextView tx_consignee_phone= ButterKnife.findById(personView,R.id.tx_consignee_phone);
            tx_consignee_phone.setText(personAddressBean.getConsigneePhone());
            TextView tx_consignee_address= ButterKnife.findById(personView,R.id.tx_consignee_address);
            tx_consignee_address.setText(String.format("收货地址：%s",personAddressBean.getConsigneeAddress()));
        }else {
            View companyView=getLayoutInflater().inflate(R.layout.serve_order_detail_address_company,null);
            frameAddress.addView(companyView);
            ServeOrderDetailBean.CompanyAddressBean companyAddressBean=serveOrderDetailBean.getCompanyAddressBean();

            TextView tx_company_name= ButterKnife.findById(companyView,R.id.tx_company_name);
            tx_company_name.setText(String.format("企业名称：%s",companyAddressBean.getCompanyName()));

            TextView tx_company_address= ButterKnife.findById(companyView,R.id.tx_company_address);
            tx_company_address.setText(String.format("企业地址：%s",companyAddressBean.getCompanyAddress()));

            TextView tx_operator_name= ButterKnife.findById(companyView,R.id.tx_operator_name);
            tx_operator_name.setText(String.format("操作人：%s",companyAddressBean.getOperatorName()));

            TextView tx_operator_phone= ButterKnife.findById(companyView,R.id.tx_operator_phone);
            tx_operator_phone.setText(companyAddressBean.getOperatorPhone());
        }
    }

    protected abstract void setDetailData(ServeOrderDetailBean serveOrderDetailBean);

    /**
     * 设置金额及支付方式
     * @param serveOrderDetailBean 界面实体
     */
    private void setOrderMoneyData(ServeOrderDetailBean serveOrderDetailBean){
        lrTotalMoney.setTvRight(MoneyUtils.withSpaceMoney(serveOrderDetailBean.getTotalMoney()));
        lrRelMoney.setTvRight(MoneyUtils.withSpaceMoney(serveOrderDetailBean.getRealMoney()));
        lrPayType.setTvRight(OrderUtils.getPayStyle(serveOrderDetailBean.getPayType(),serveOrderDetailBean.getIsPay()));//支付方式
        lrRedPacket.setTvRight(serveOrderDetailBean.getRedPacketMoney()==0?"未使用":MoneyUtils.withSpaceMoney(serveOrderDetailBean.getRedPacketMoney()));
    }

    /**
     * 设置发票信息
     * @param serveOrderDetailBean 界面实体
     */
    private void setOrderInvoiceData(ServeOrderDetailBean serveOrderDetailBean){
        String invoiceType=StringUtils.getInvoiceTypeStr(serveOrderDetailBean.getInvoiceType());
        if(TextUtils.isEmpty(invoiceType)){
            lrInvoiceType.setVisibility(View.GONE);
            lrInvoiceName.setVisibility(View.GONE);
        }else {
            lrInvoiceType.setVisibility(View.VISIBLE);
            lrInvoiceName.setVisibility(View.VISIBLE);
        }
        lrInvoiceType.setTvRight(invoiceType);
        lrInvoiceName.setTvRight(serveOrderDetailBean.getInvoiceHead());
    }

    /**
     * 设置底部状态按钮：目前是仅当待付款情况显示按钮，且可以去支付等
     * @param serveOrderDetailBean 界面实体
     */
    private void setOrderStatusData(ServeOrderDetailBean serveOrderDetailBean){
        frameOrderStatus.removeAllViews();
        if(ServeOrderDetailBean.Order_Status_Need_Pay.equalsIgnoreCase(serveOrderDetailBean.getOrderStatus())){
            View statusView=getLayoutInflater().inflate(R.layout.serve_order_detail_status,null);
            frameOrderStatus.addView(statusView);
            ButterKnife.findById(statusView,R.id.tx_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new MaterialDialog.Builder(ServeOrderBaseDetailActivity.this).content("确定取消订单信息吗").onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            cancelOrder();
                        }
                    }).positiveText("确认").negativeText("取消").show();
                }
            });
            ButterKnife.findById(statusView,R.id.tx_pay).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    payOrder();
                }
            });
        }
    }

    /**
     * 取消订单
     */
    protected abstract void cancelOrder();

    /**
     * 订单支付
     */
    protected abstract void payOrder();
}
